﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net.Sockets;
using System.Net;

using log4net;
namespace DomainSearchTools.Common
{
    public class SocketHelper
    {
        private static ILog logger = LogManager.GetLogger(typeof(SocketHelper));
     
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="server">socket服务器</param>
        /// <param name="port">端口</param>
        /// <param name="content">内容</param>
        /// <returns></returns>
        public string Send(string server, int port, string content)
        {
            //1.连接服务器
            using (Socket socket = ConnectSocket(server, port))
            {
                if (socket == null)
                {
                    throw new Exception("连接服务器失败");
                }

                byte[] data = Encoding.UTF8.GetBytes(content);
                //3.发送数据
                socket.Send(data, data.Length, SocketFlags.None);

                //4.接受数据
                data = new byte[204800];
                int index = socket.Receive(data, data.Length, SocketFlags.None);
                string response = Encoding.UTF8.GetString(data, 0, index);
                return response;

                //待处理  发布去除
                logger.Info(server + @"
" + response.Replace("\n", "\r\n"));

            }
        }

        /// <summary>
        /// Socket连接服务器
        /// </summary>
        /// <param name="server"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        private Socket ConnectSocket(string server, int port)
        {
            Socket s = null;
            IPHostEntry hostEntry = Dns.GetHostEntry(server);
            foreach (IPAddress address in hostEntry.AddressList)
            {
                IPEndPoint ipe = new IPEndPoint(address, port);
                Socket tempSocket =
                    new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                tempSocket.Connect(ipe);

                if (tempSocket.Connected)
                {
                    s = tempSocket;
                    break;
                }
                else
                {
                    continue;
                }
            }
            return s;
        }

        TcpClient client = new TcpClient();
        /// <summary>
        /// Tcp的形式发送数据
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="content"></param>
        public void TcpSend(string ip, int port, string content)
        {
            //1.连接服务器
            client.Connect(IPAddress.Parse(ip), port);
            //2.创建数据流内容
            var stream = client.GetStream();

            byte[] data = Encoding.UTF8.GetBytes(content);
            //3.发送数据
            stream.Write(data, 0, data.Length);

            //4.接受数据
            data = new byte[1024];
            int index = stream.Read(data, 0, data.Length);
            string response = Encoding.UTF8.GetString(data, 0, index);
            // textBox1.AppendText(response);

            stream.Close();
            client.Close();


            ////3.发送数据
            //StreamWriter sw = new StreamWriter(stream);
            //sw.Write(content);
            //sw.Close();

            ////4.接收数据
            //StreamReader sr = new StreamReader(stream);
            //string response = sr.ReadToEnd();
            //textBox1.AppendText(response);
            //sr.Close();
        }
    }
}
